data-types
Data Types in JavaScript:#
- String
- Number
- Boolean
- null
- undefined
- Symbol (new in ES2015)
- Object
- Function
- Array
- Date
- RegExp
JavaScript is known as a "weakly" typed language. What this means is that when you create variables and assign them values, you do not have to specify the type of data you are working with like statically (or strongly) typed languages, like Java and C++, you do need to specify the type.
Now let's look at data types a little more.
String#
A string is a set of characters enclosed in quotes and can be defined using single or double quotes:
So what is the difference between the two ways of initializing a string? Well, first of all, if you want quotes in your string, it's nice to have another option to start and end the string:
What would happen if you try to use double quotes to create the previous string instead of using single quotes? Try it in your console
Notice there is a backslash before the single quote in haven't. It's called escape character and it tells JavaScript that the single quote in the string should not be used to end the string. Try removing the backslash from the string and seeing what happens in your JavaScript console.
To find the number of characters(length) of a string, access its length property:
Number#
JavaScript numbers can be positive, negative or decimal numbers or zero:
๐ก Zero in javascript can be positive or negative (+0 or -0) and there are rare cases for negative zero (ex: deciding on the direction of standing person without moving like the direction that he is looking at could be either North +0 or South -0).
The standard arithmetic operators are supported, including addition, subtraction, modulus (or remainder) arithmetic, and so forth.
There's also a built-in object that we did not mention earlier called Math that provides advanced mathematical functions and constants:
๐ก We will dive deep on the coming sessions on what is a function and how we can use it
JavaScript also has the special values Infinity and -Infinity:
Boolean#
A boolean type can only be in one of two states, true or false (both of which are keywords) .
Boolean types are a very useful tool for controlling our program. For example, if a user is signed in, you might want to show them a link to update their profile; but if a user is not logged in, you'd probably just want to show them a sign-in link. This sort of behavior, where the code that gets executed is conditioned on something else, happens all the time in programming. We'll learn more about how to deal with these situations in the next sessions.
You can test for Infinity, -Infinity values using the built-in isFinite() function:
null#
A value that indicates a deliberate non-value (and is only accessible through the null keyword).It signifies an intentional absence of data.
undefined#
a value of type undefined that indicates an uninitialized variable โ that is, a value hasn't even been assigned yet.
You can also explicitly set a variable to undefined:
๐ก If you want to reset a value then you can assign
nullto the variable.
Symbol#
The Symbol() function returns a value of type symbol, Every symbol value returned from Symbol() is unique . A symbol value may be used as an identifier for object properties, this is the data type's primary purpose. To create a new primitive symbol, you write Symbol() with an optional string as its description:
๐ก We use double equals
==and triple equals===for comparison, we will see them more on the coming sessions
Figuring out a variable's type in JavaScript#
In JavaScript, we have a keyword called typeof that returns the type of the variable
๐ก The type of
nullas an object was a bug in the language and it is hard to change after all these years, it was supposed to be of type "null".
Converting between types#
Sometimes you'll need to convert a value from one type to another. For example, maybe you want to do some math on a couple of numbers, but you get the numbers from another source and it has a value of string so you need to convert this string to number so what would you do?
Examples:#
Converting to a number
There are several ways you can convert a value to a number. One way is to parse the number, using parseInt, parseFloat or Number function that it doesn't parse, it simply tries to convert the entire string directly to a number.
๐ก In addition to the methods above you can also use the plus operator(+) to convert a string to a number by putting the plus in front of the string and it will act as the
Number()function, you can use it like this+"3.5"and the result will be3.5as a number
Converting to a string
The toString method will convert any value which is not undefined or null into a string.
๐ก You can't use
toStringdirectly on the number25.toString()โ and you can only use it on the variable.
Practice Time ๐จโ๐ป#
Follow the following instructions, then write your code inside the index file.
Now let's practice
NumbersandConcatenation๐ค. Follow the instructions, then write your code inside the index file.